import { EntityManager, MikroORM } from '@mikro-orm/core';
import { InjectEntityManager, InjectMikroORM } from '@mikro-orm/nestjs';
import { Body, Controller, Get, Post, Query, UploadedFile, UseInterceptors } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JsonApiResponse } from '@utils/json.api.response';
import { knext } from 'knext';
import { ImsYzdKpStorage } from '@databases/entities/ims_yzd_kp/ImsYzdKpStorage';
import { ImsYzdKpStorageGroup } from '@databases/entities/ims_yzd_kp/ImsYzdKpStorageGroup';
import { group } from 'console';
import { ImsYzdKpStorageList } from '@databases/entities/ims_yzd_kp/ImsYzdKpStorageList';
import { type } from 'os';
import { FileInterceptor } from '@nestjs/platform-express';
import dayjs from 'dayjs';

@Controller('media')
export class MediaController {
    constructor(
        private readonly configService: ConfigService,
        @InjectMikroORM('ims_yzd_kp')
        private readonly orm: MikroORM,
        @InjectEntityManager('ims_yzd_kp')
        private readonly em: EntityManager, 
    ){}

    @Post('group_list')
    async typeList() {
        const knex = knext(this)
        const results = await knex.fromORM(ImsYzdKpStorageGroup).where({pid:0})
        return JsonApiResponse.object(results)
    }

    @Get('media_list')
    async mediaList(
        @Query('type') type = 1,
        @Query('no') no = 1,
        @Query('pagesize') pagesize = 10,
        @Query('title') title = '',
        @Query('group') group_id?
    ) {
        const knex = knext(this)
        const results = await knex.fromORM(ImsYzdKpStorageList)
        .where({type})
        .where({group_id:group_id?group_id:0})
        .whereILike('title',`%${title}%`)
        .orderBy('id','desc')
        .paginate(no,pagesize)

        return JsonApiResponse.object(results)
    }

    @Post('media_list')
    async mediaListPost(
        @Body('type') type = 1,
        @Body('page') no = 1,
        @Body('pagesize') pagesize = 10,
        @Body('title') title = '',
        @Body('group') group_id?
    ) {
         const knex = knext(this)
         const qb = knex.fromORM(ImsYzdKpStorageList)
        .where({type})
        .where({group_id:group_id?group_id:0})
        .whereILike('title',`%${title}%`)
        .orderBy('id','desc')
        
        const results = await qb.cloneORM().paginate(no,pagesize)
        const {total} = await qb.cloneORM().count({'total':'*'}).getOne()

        return JsonApiResponse.object({list:results,total:total})
    }

    @Post('save')
    @UseInterceptors(FileInterceptor('file'))
    async save(
        @Body('uniacid') uniacid = 1,
        @Body('uid') uid = 1,
        @Body('type') type,
        @Body('oss_type') oss_type = 1,
        @Body('group_id') group_id = 0,
        @Body('url') url,
        @Body('path') path,
        @Body('width') width?,
        @Body('height') height?,
        @Body('size') size = 0,
        @Body('title') title='',
        @Body('data') data = '',
        @Body('duration') duration = 0
        ) {

        const knex = knext(this);

        await knex.fromORM(ImsYzdKpStorageList)
        .insert({
            group_id:group_id,
            url:url,
            path:path,
            type:type,
            uniacid:uniacid,
            title:title,
            size: size,
            uid:uid,
            oss_type:oss_type,
            width:width,
            height:height,
            duration:duration = 0,
            data:data,
            add_time:dayjs().unix(),
            update_time:dayjs().unix()
        })

        return JsonApiResponse.OK()
    }
}
